import {BaseApi} from "./BaseApi.ts";
import {buildOldName, buildUserDetail, buildUserDetailHtml} from "../types/User.ts";
import {BlockConfig} from "../types/BlockConfig.ts";

export class UserApi extends BaseApi {
    info = (uid?: number, username?: string) => this.getNuke({uid, username, __act: "get",})
        .then(res => buildUserDetail(res))

    oldName = (uid: number) => this.getNuke({uid, __act: "oldname",})
        .then(res => res.map((item: any) => buildOldName(item)))

    infoHtml = (uid?: number, username?: string) => this.instance.get("nuke.php", {
        params: {
            uid,
            username,
            func: "ucp",
        },
    }).then(res => buildUserDetailHtml(res.data))

    blockConfigGet = (uid?: number | undefined) => this.postNuke({__act: "get_block_word", uid})
        .then(res => new BlockConfig(res[0]))

    blockConfigSet = (config: BlockConfig) => this.postNuke({__act: "set_block_word", data: config.toString()})
        .then(res => res[0])

    // 修改签名
    signatureSet = (uid: number, sign: string) => this.instance.post("nuke.php", {
        __lib: "set_sign", __act: "set",
        uid, sign, __output: 11
    })
        .then(res => res.data.data)

    getNuke = (params: any) => this.instance.get("nuke.php", {
        params: {
            ...params,
            __output: 11,
            __lib: "ucp",
        },
    }).then(res => res.data.data)

    postNuke = (params: any) => this.instance.post("nuke.php", {
        ...params,
        __output: 11,
        __lib: "ucp",
    }).then(res => res.data.data)
}